# 1.使用类属性
# class Seasons:
#     Spring = 0
#     Summer = 1
#     Autumn = 2
#     Winter = 3

# 简化
# class Seasons:
#     Spring, Summer, Autumn, Winter = range(4)


# 2.借助函数

# def enum(*posarg, **keysarg):
#     return type('Enum', (object,), dict(zip(posarg, range(len(posarg))), **keysarg))
#
# Seasons = enum('Spring', 'Summer', 'Autumn', Winter=1)
# print(Seasons.Autumn)


# 3.使用collections.namedtuple
# from collections import namedtuple
#
# # 可以允许值相等
# Seasons = namedtuple('Season', 'Spring Summer Autumn Winter')._make([1, 2, 2, 4])
# print(Seasons)
# # Season(Spring=1, Summer=2, Autumn=3, Winter=4)
#
# # 支持无意义的运算
# print(Seasons.Spring + Seasons.Summer)
#
# # OrderedDict([('Spring', 1), ('Summer', 2), ('Autumn', 2), ('Winter', 4)])
# print(Seasons._asdict())
#
# # 修改属性
# seasons = Seasons._replace(Spring=6)
# print(seasons.Spring)

# 4.使用枚举类

from enum import Enum

Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
for name, member in Month.__members__.items():
    print(name, '=>', member, ',', member.value)

from enum import Enum, unique


@unique
class Weekday(Enum):
    Sun = 0  # Sun的value被设定为0
    Mon = 1
    Tue = 2
    Wed = 3
    Thu = 4
    Fri = 5
    Sat = 6


@unique
class Gender(Enum):
    Male = 0
    Female = 1


class Student(object):
    def __init__(self, name, gender):
        self.name = name
        if isinstance(gender, Gender):
            self.gender = gender
        else:
            raise ValueError('只允许接收Gender类型的参数')


bart = Student(name='dai', gender=Gender.Male)
if bart.gender == Gender.Male:
    print('测试通过!')
else:
    print('测试失败!')
